package com.nulldev.util.graphics.renderIt.api.utils;

import java.util.Timer;
import java.util.TimerTask;

import com.nulldev.util.concurrency.threadIt.v4.locking.SignalBarrier;
import com.nulldev.util.gui.swingAPI.Swing;

public class RITVSyncTimer {

	private static final RITVSyncTimer timer = new RITVSyncTimer();

	private final long displayHz;
	private final Timer t;
	private final SignalBarrier sb = new SignalBarrier();

	public RITVSyncTimer() {
		this.displayHz = Swing.refreshRate();
		this.t = new Timer("RIT-VSYNC");
		this.t.scheduleAtFixedRate(new TimerTask() {

			@Override
			public void run() {
				sb.signal();
			}
		}, 0L, (1000L / displayHz));
	}

	public static RITVSyncTimer get() {
		return timer;
	}

	public void waitForBlank() {
		try {
			sb.await();
		} catch (InterruptedException e) {
		} catch (IllegalStateException e2) {
		}
	}
}
